package com.lwlean.test.quartz;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Created by lean on 2016/12/22.
 */
@Component
@EnableScheduling
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

//    @Autowired
//    private OSSConfiguration ossConfiguration;
//
//    @Autowired
//    private OSSUploadService ossUploadService;
    /**
     * 固定速率
     */
//    @Scheduled(fixedRate = 1000 * 60)
    public void testFixedRate(){
        log.info("into quartz");
//        RestTemplate restTemplate = new RestTemplate();
//        log.info("into quartz");
////        final String url = "http://server02.dmcld.com:3030/api/send_sms?sn=DB01-6B09-0200-0097";
////        final String url = "http://server02.dmcld.com:3030/api/set_port_info?port=8\\&action=unblock\\&number=15851463036?sn=DB01-6B09-0200-0097";
////        final String message = "{\"text\":\"#param#\",\"port\":[8],\"param\":[{\"number\":\"10086\",\"text_param\":[\"111\"],\"user_id\":3}]}";
////        setRestTemplate(url,message,HttpMethod.GET);
//        Map<String,Object> var = new HashedMap();
//        var.put("port",8);
//        var.put("action","unblock");
//        var.put("number","15851463036");
//        final String url = "http://server02.dmcld.com:3030/api/set_port_info?sn=DB01-6B09-0200-0097";
//        final String result = restTemplate.getForObject(url, String.class, var);
//        log.info("get result:{}",result);
    }

    /**
     * 定时 优先级高于fixedRate
     */
//    @Scheduled(cron = "40 06 00 * * *")
//    public void testCron(){
//
//    }

    /**
     * 定时上传至oss
     */
//    @Scheduled(cron = " 0 0 * * * *")
//    public void testOSSUpload(){
//        final OSS oss = ossConfiguration.getOSS();
//        ossUploadService.ossUpload(oss);
//    }

    /**
     * 发送短信方法
     * @param parameters
     * @param uri
     */
    private void setRestTemplate(String uri,String parameters,HttpMethod method){

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        //设置防止传递中文为乱码
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.setAcceptCharset(Arrays.asList(StandardCharsets.UTF_8));
        HttpEntity<String> entity = null;
        log.info("use url:{}",uri);
        log.info("use method:{}",method);
        if (HttpMethod.POST == method){
            entity = new HttpEntity<String>(parameters, headers);
        } else if (HttpMethod.GET == method) {
            entity = new HttpEntity<String>(headers);
        }
        ResponseEntity<String> result = restTemplate.exchange(uri, method, entity, String.class);
        final HttpStatus statusCode = result.getStatusCode();
        final String body = result.getBody();
        log.info("send result:{}",statusCode);
    }
}
